package cn.kust.chat.controller;

import cn.kust.chat.entity.ChatMessage;
import cn.kust.chat.entity.ChatUserBind;
import cn.kust.chat.mapper.ChatMessageMapper;
import cn.kust.chat.mapper.ChatUserBindMapper;
import cn.kust.chat.pojo.MsgBindRel;
import cn.kust.chat.utils.JsonResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/chat")
@CrossOrigin
@Api(tags = "聊天模块")
public class MsgExController {

    @Resource
    ChatMessageMapper chatMessageMapper;

    @Resource
    ChatUserBindMapper chatUserBindMapper;

    @PostMapping("/msgEx")
    @ApiOperation("查询聊天记录")
    @ApiImplicitParams({@ApiImplicitParam(name = "sendUserId", value = "发送者id", defaultValue = "1"),
            @ApiImplicitParam(name = "receiveUserId", value = "接收者id", defaultValue = "2")
    })
    public JsonResult<List<ChatMessage>> getMsgEx(@RequestBody MsgBindRel msgBindRel){
        Long sendUserId=msgBindRel.getSendUserId();
        Long receiveUserId=msgBindRel.getReceiveUserId();

        // 查询绑定编号
        ChatUserBind chatUserBind = chatUserBindMapper.selectOne(new LambdaQueryWrapper<ChatUserBind>()
                .and(i -> i.eq(ChatUserBind::getUserIdOne, sendUserId).or().eq(ChatUserBind::getUserIdTwo, sendUserId))
                .and(i -> i.eq(ChatUserBind::getUserIdOne, receiveUserId).or().eq(ChatUserBind::getUserIdTwo, receiveUserId)));
        if (chatUserBind!=null){
            System.out.println(chatUserBind.getBindId());
            Long bindId = chatUserBind.getBindId();
            //查询聊天记录
            List<ChatMessage> msgEx=chatMessageMapper.selectList(new LambdaQueryWrapper<ChatMessage>()
                    .eq(ChatMessage::getBindId,bindId)
                    .orderByAsc(ChatMessage::getCreateTime));
            return JsonResult.ok(msgEx);
        }

        return null;

    }
}
